// pages/driver/order/index/index.js
const app = getApp();
const service = require('../../../../utils/service.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 状态
    navIdx: 1,
    // 订单列表
    orderList: [],
    // 待接订单
    noReceiveOrderData: {
      list: [],
      currentPage: 0
    },
    // 已接订单
    receivedOrderData: {
      list: [],
      currentPage: 0
    },
    // 已完成订单
    completeOrderData: {
      list: [],
      currentPage: 0
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
  
  },

  // 加载未接订单
  loadNoReceiveOrderList() {
    var that = this;
    service.getConnect(service.api.getDriverNoReceiveOrder, {
      page: this.data['noReceiveOrderData']['currentPage'] + 1
    }).then(function(res) {
      wx.hideLoading();
      that.setData({
        noReceiveOrderData: res.data
      });
      if(that.data.navIdx===1){
        that.setData({
          orderList: that.data.orderList.concat(res.data.list)
        })
      }
    })
  },

  // 加载已接订单
  loadReceivedOrderList() {
    var that = this;
    service.getConnect(service.api.getDriverReceivedOrder, {
      page: this.data['receivedOrderData']['currentPage'] + 1
    }).then(function(res) {
      wx.hideLoading();
      that.setData({
        receivedOrderData: res.data
      });
      if (that.data.navIdx === 2) {
        that.setData({
          orderList: that.data.orderList.concat(res.data.list)
        })
      }
    })
  },

  // 加载已完成订单
  loadCompleteOrderList() {
    var that = this;
    service.getConnect(service.api.getDriverCompleteOrder, {
      page: this.data['completeOrderData']['currentPage'] + 1
    }).then(function(res) {
      wx.hideLoading();
      that.setData({
        completeOrderData: res.data
      });
      if (that.data.navIdx === 3) {
        that.setData({
          orderList: that.data.orderList.concat(res.data.list)
        })
      }
    })
  },

  // 选择
  bindTapNav(e) {
    let params = '';
    if (e.currentTarget.dataset.idx === '1') {
      params = 'noReceiveOrderData';
    } else if (e.currentTarget.dataset.idx === '2') {
      params = 'receivedOrderData';
    } else {
      params = 'completeOrderData';
    }
    this.setData({
      navIdx: parseInt(e.currentTarget.dataset.idx),
      orderList: this.data[params]['list']
    })
  },

  // 地图查看
  bindToLocation(e) {
    console.log(e.currentTarget.dataset);
    let latitude = parseFloat(e.currentTarget.dataset.latitude);
    let longitude = parseFloat(e.currentTarget.dataset.longitude);
    if(latitude===0&&longitude===0){
      return;
    }
    wx.openLocation({
      latitude: latitude,
      longitude: longitude,
      scale: 18,
      address: e.currentTarget.dataset.address
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      orderList: [],
      // 待接订单
      ['noReceiveOrderData.list']: [],
      ['noReceiveOrderData.currentPage']: 0,
      // 已接订单
      ['receivedOrderData.list']: [],
      ['receivedOrderData.currentPage']: 0,
      // 已完成订单
      ['completeOrderData.list']: [],
      ['completeOrderData.currentPage']: 0
    })
    this.loadNoReceiveOrderList();
    this.loadReceivedOrderList();
    this.loadCompleteOrderList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      orderList: [],
      // 待接订单
      ['noReceiveOrderData.list']: [],
      ['noReceiveOrderData.currentPage']: 0,
      // 已接订单
      ['receivedOrderData.list']: [],
      ['receivedOrderData.currentPage']: 0,
      // 已完成订单
      ['completeOrderData.list']: [],
      ['completeOrderData.currentPage']: 0
    })
    this.loadNoReceiveOrderList();
    this.loadReceivedOrderList();
    this.loadCompleteOrderList();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    let wxappStyle = wx.getStorageSync('wxappStyle');
    return {
      title: app.globalData['wxappStyle']['wxappTitle'] || wxappStyle['wxappTitle'],
      path: '/pages/index/index'
    }

  }
})
